package com.example.acceletracks;

import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

//import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Display;
import android.view.Window;

public class RouteActivity extends android.support.v4.app.FragmentActivity {
	
	private GoogleMap mMap;
	private Route route;
	private AcceleHoles acceleHoles;
	private LatLng startPoint;
	private LatLng endPoint;
	private PolylineOptions polyline;
	private LatLngBounds bounds;
	private String route_name;
	
	private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL, GoogleMap.MAP_TYPE_HYBRID,
		 GoogleMap.MAP_TYPE_SATELLITE};
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.route_activity);
           
        setUpActivity();
        setUpRoute();
        setUpMap();
    }
	
	private void drawPoints() {

		if (acceleHoles != null) {
			for (int i = 0; i < acceleHoles.getLength(); i++) {
				LatLng latlng = (LatLng) acceleHoles.toArray()[i];
				mMap.addMarker(new MarkerOptions().position(latlng).icon(
						BitmapDescriptorFactory
								.fromResource(R.drawable.point_blackk)));
			}
			
		}

	}

	private void setUpRoute() {

		if (route != null) {

			List<LatLng> points = route.getPoints();

			startPoint = route.getStartPoint();
			endPoint = route.getEndPoint();

			polyline = new PolylineOptions().addAll(points).color(Color.RED)
					.width(8).geodesic(true);
		}
	}

	private void setUpActivity() {
		if (route == null || acceleHoles == null) {

			Bundle type = getIntent().getExtras();
			if (type != null)
				if (type.containsKey("route_name"))
					route_name = type.getString("route_name");

			String route_string = IOManager.readFileAsString(route_name
					.concat(".track"), "_MyTrack");
			String points_string = IOManager.readFileAsString(route_name
					.concat(".hole"), "_MyAcceleHoles");

			route = new Route(IOManager.convertStringToLatLngList(route_string));
			acceleHoles = new AcceleHoles(IOManager.convertStringToLatLngList(points_string));


		}

	}

	private void setUpMap() {
		
		if (mMap == null) {

			mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
					R.id.map)).getMap();
			
			if (mMap != null && route!=null)
			{
				int id=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("maptype", "0"));
				mMap.setMapType(MAP_TYPES[id]);
				
				mMap.setOnMapLongClickListener(new MyOnMapLongClickListener(this));
				mMap.addMarker(new MarkerOptions().position(startPoint).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
				mMap.addPolyline(polyline);
				mMap.addMarker(new MarkerOptions().position(endPoint).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
				
				drawPoints();
				
				
				bounds = buildLatLngBounds(); 			
				
				int width = getScreenSize().x;
				int height = getScreenSize().y;
				
				mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, 80));

			}
		}
	}
	
	private LatLngBounds buildLatLngBounds()
	{
		LatLngBounds.Builder builder = new LatLngBounds.Builder();
		
		List<LatLng> points = route.getPoints();
		
		for(int i=0; i<points.size(); i++)
			builder.include(points.get(i));
		
		return builder.build();
	}
	
	public Point getScreenSize()
	{
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		size.set(display.getWidth(), display.getHeight());
		//display.getSize(size);

		return size;
	}
	
	
	public String getRoute_name() {
		return route_name;
	}

}
